import axios from './../utils/http';

import $store from './../store';
class buyTicket {
  // 得到购票的状态，
  getTicketState(corpId, activityId) {
    return new Promise((resolve) => {
      let resolveData = {
        success: false,
        errorMsg: '',
        result: []
      };
      // 注意
      // 去掉服务端数据时候，同步一下其他的
      // let resData = {
      //   "success": true,
      //   "errorMsg": null,
      //   "result": {
      //     "activityId": 1,
      //     "title": "UCAN设计大会",
      //     "address": "",
      //     "isEnable": 0,
      //     "startDate": 1550764800000,
      //     "enddate": 1551369599000,
      //     "corpId": "ding3693d292f695a58d35c2f4657eb6378f",
      //     "createDate": 1568996330000,
      //     "updateDate": 1550804841000,
      //     "startSaleDate": 1548999951000,
      //     "countdownDate": 1538999151000,
      //     // --
      //     // "createDate": 1548999890000,
      //     // "updateDate": 1550804841000,
      //     // "startSaleDate": 1548999951000,
      //     // "countdownDate": 1538999151000,
      //     "limitNum": 15,
      //     "connectEmail": "1123",
      //     "customerServiceTel": "13245678",
      //     "resources": null
      //   }
      // };
      // if (resData.success) {
      //   $store.state.buyTicket.activityTitle = resData.result.title;
      //   $store.state.buyTicket.getTicketState = resData.result || {};
      //   sessionStorage.setItem("getTicketState", JSON.stringify(resData.result));
      //   $store.state.buyTicket.groupBuyingLimitNum = resData.result.groupNum || 1; //团购订单数量
      //   console.log($store.state.buyTicket.getTicketState.groupBuyingLimitNum);

      //   resolveData.success = true;
      //   resolveData.result = resData.result;
      //   $store.state.buyTicket.startSaleDate = resData.result.startSaleDate || 0; //开售时间
      //   $store.state.buyTicket.countdownDate = resData.result.countdownDate || 0; //倒计时时间
      //   $store.state.buyTicket.createDate = resData.result.createDate || 0 || new Date().getTime(); //当前时间
      //   $store.state.buyTicket.limitNum = Number(resData.result.limitNum) || 1; //限购数量
      //   if ($store.state.buyTicket.limitNum <= 1) {
      //     $store.state.buyTicket.limitNum = 1;
      //   }
      //   $store.state.buyTicket.connectEmail = resData.result.connectEmail || ""; //客服邮箱
      //   sessionStorage.setItem('connectEmail', $store.state.buyTicket.connectEmail);
      //   $store.state.buyTicket.customerServiceTel = resData.result.customerServiceTel || ""; //客服电话
      //   sessionStorage.setItem('customerServiceTel', $store.state.buyTicket.customerServiceTel);
      //   //是否已开售,true已开售
      //   if ($store.state.buyTicket.startSaleDate < $store.state.buyTicket.createDate) {
      //     $store.state.buyTicket.noSale = true;
      //   } else {
      //     $store.state.buyTicket.noSale = false;
      //   }
      //   //是否显示倒计时 true 显示
      //   if ($store.state.buyTicket.countdownDate <= $store.state.buyTicket.createDate && $store.state.buyTicket.createDate < $store.state.buyTicket.startSaleDate) {
      //     $store.state.buyTicket.ifShowTimeDown = true;
      //   } else {
      //     $store.state.buyTicket.ifShowTimeDown = false;
      //   }

      //   // 剩余时间 开售时间-当前时间=剩余时间的毫秒数
      //   let nowDate = $store.state.buyTicket.createDate;
      //   let endDate = $store.state.buyTicket.startSaleDate;
      //   $store.state.buyTicket.surplusData =
      //     ((Number(endDate) || 0) - (Number(nowDate) || 0)) / 1000; //多少秒
      //   // $store.state.buyTicket.noSale = false; //为了测试
      //   // $store.state.buyTicket.limitNum = 8; //为了测试
      // } else {
      //   resolveData.errorMsg = resData.errorMsg || '数据获取失败！';
      // }
      // resolve(resolveData);
      // return;
      let subData = {
        corpId: corpId,
        activityId: activityId,
      };
      axios.post('ticketPurchaseMob/queryActivityById',
        subData,
      ).then(resData => {
        if (resData.success) {
          $store.state.buyTicket.activityTitle = resData.result.title;
          $store.state.buyTicket.getTicketState = resData.result || {};
          sessionStorage.setItem("getTicketState", JSON.stringify(resData.result));
          $store.state.buyTicket.groupBuyingLimitNum = resData.result.groupNum || 1; //团购订单数量
          resolveData.success = true;
          resolveData.result = resData.result;
          $store.state.buyTicket.startSaleDate = resData.result.startSaleDate || 0; //开售时间
          $store.state.buyTicket.countdownDate = resData.result.countdownDate || 0; //倒计时时间
          $store.state.buyTicket.createDate = resData.result.createDate || 0 || new Date().getTime(); //当前时间
          $store.state.buyTicket.limitNum = Number(resData.result.limitNum) || 1; //限购数量
          if ($store.state.buyTicket.limitNum <= 1) {
            $store.state.buyTicket.limitNum = 1;
          }
          $store.state.buyTicket.connectEmail = resData.result.connectEmail || ""; //客服邮箱
          sessionStorage.setItem('connectEmail', $store.state.buyTicket.connectEmail);
          $store.state.buyTicket.customerServiceTel = resData.result.customerServiceTel || ""; //客服电话
          sessionStorage.setItem('customerServiceTel', $store.state.buyTicket.customerServiceTel);
          //是否已开售,true已开售
          if ($store.state.buyTicket.startSaleDate < $store.state.buyTicket.createDate) {
            $store.state.buyTicket.noSale = true;
          } else {
            $store.state.buyTicket.noSale = false;
          }
          //是否显示倒计时 true 显示
          if ($store.state.buyTicket.countdownDate <= $store.state.buyTicket.createDate && $store.state.buyTicket.createDate < $store.state.buyTicket.startSaleDate) {
            $store.state.buyTicket.ifShowTimeDown = true;
          } else {
            $store.state.buyTicket.ifShowTimeDown = false;
          }

          // 剩余时间 开售时间-当前时间=剩余时间的毫秒数
          let nowDate = $store.state.buyTicket.createDate;
          let endDate = $store.state.buyTicket.startSaleDate;
          $store.state.buyTicket.surplusData =
            ((Number(endDate) || 0) - (Number(nowDate) || 0)) / 1000; //多少秒
          // $store.state.buyTicket.noSale = false; //为了测试
          // $store.state.buyTicket.limitNum = 8; //为了测试
        } else {
          resolveData.errorMsg = resData.errorMsg || '数据获取失败！';
        }
        resolve(resolveData);
      }, err => {
        resolveData.errorMsg = '服务器出小差了！';
        resolve(resolveData);
      });
    });
  }
  // 得到门票基础数据
  getTicketList(linkId) {
    return new Promise((resolve) => {
      let resolveData = {
        success: false,
        errorMsg: '',
        result: []
      };
      // {"success":true,"errorCode":null,"errorMsg":null,"result":{"head":{"pricegroupId":11,"projectId":23,"ptype":0,"status":0,"pname":"价格组名称","vnote":"备注","updateTime":1559013098000,"createTime":1554980314000,"isDel":0},"corpId":"ding42d297c548f1006335c2f4657eb6378f","agereeMents":[{"agreementId":32,"activityId":23,"typeId":1,"corpId":"ding42d297c548f1006335c2f4657eb6378f","title":"11","isDel":0,"vnote1":null,"vnote2":null,"vnote3":null,"vnote4":null,"vnote5":null,"vnote6":null,"vnote7":null,"vnote8":null,"vnote9":null,"vnote10":"pay","createTime":1556503708000,"updateTime":1556503708000,"xyvote":"<p>1</p>","isPay":null},{"agreementId":33,"activityId":23,"typeId":2,"corpId":"ding42d297c548f1006335c2f4657eb6378f","title":"22","isDel":0,"vnote1":null,"vnote2":null,"vnote3":null,"vnote4":null,"vnote5":null,"vnote6":null,"vnote7":null,"vnote8":null,"vnote9":null,"vnote10":"pay","createTime":1559013967000,"updateTime":1559013967000,"xyvote":"<p>22</p>","isPay":null}],"body":[{"pricegroupbId":51,"pricegroupId":11,"ticketId":50,"exclusive":null,"vnote":null,"updateTime":1559110361000,"createTime":1554980315000,"isDel":0,"status":0,"enote":"折扣标签","snote":"价格标签","inum":97,"snum":1,"eprice":0.1,"sprice":100.0,"ableNum":null,"frozenNum":2,"tname":"aa","detail":"[\"aa\",\"assa\",\"as\",\"asasazczcs\",\"啊啊啊啊啊啊啊啊\",\"啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊\",\"啊啊啊啊啊啊啊啊啊啊\",\"啊啊啊啊啊啊啊啊啊啊\"]","address":"aaa","stime":1554566400000,"etime":1555344000000,"tTypeDate":"06:04-06:10","ticketUrl":"https://static.dingtalk.com/media/lALPDeC2t_ucqWvNAQ7NAQ4_270_270.png"},{"pricegroupbId":61,"pricegroupId":11,"ticketId":55,"exclusive":null,"vnote":null,"updateTime":1559110361000,"createTime":1556503750000,"isDel":0,"status":0,"enote":"折扣标签2","snote":"价格标签2","inum":98,"snum":1,"eprice":0.2,"sprice":100.0,"ableNum":null,"frozenNum":1,"tname":"票名","detail":"[\"地址\",\"地址\"]","address":"地址","stime":1555430400000,"etime":1556121600000,"tTypeDate":"06:03-06:46","ticketUrl":"https://static.dingtalk.com/media/lALPDgQ9qphGYxzNAoDNAoI_642_640.png"},{"pricegroupbId":62,"pricegroupId":11,"ticketId":56,"exclusive":null,"vnote":null,"updateTime":1559110361000,"createTime":1556503820000,"isDel":0,"status":0,"enote":"折扣标","snote":"价格标","inum":98,"snum":1,"eprice":0.2,"sprice":100.0,"ableNum":null,"frozenNum":1,"tname":"啊啊啊","detail":"[\"啊\"]","address":"啊啊","stime":1556035200000,"etime":1556640000000,"tTypeDate":"06:02-06:04","ticketUrl":"https://static.dingtalk.com/media/lALPDgQ9qphIZ0rNAoDNAoI_642_640.png"}]},"arguments":null}
      // 注意
      // 去掉服务端数据时候，同步一下其他的
      // let resData = {
      //   "success": true,
      //   "errorCode": null,
      //   "errorMsg": null,
      //   "result": {
      //     "head": {
      //       "pricegroupId": 11,
      //       "projectId": 23,
      //       "ptype": 0,
      //       "status": 0,
      //       "pname": "价格组名称",
      //       "vnote": "备注",
      //       "updateTime": 1559013098000,
      //       "createTime": 1554980314000,
      //       "isDel": 0
      //     },
      //     "corpId": "ding42d297c548f1006335c2f4657eb6378f",
      //     "agereeMents": [{
      //       "agreementId": 32,
      //       "activityId": 23,
      //       "typeId": 1,
      //       "corpId": "ding42d297c548f1006335c2f4657eb6378f",
      //       "title": "11",
      //       "isDel": 0,
      //       "vnote1": null,
      //       "vnote2": null,
      //       "vnote3": null,
      //       "vnote4": null,
      //       "vnote5": null,
      //       "vnote6": null,
      //       "vnote7": null,
      //       "vnote8": null,
      //       "vnote9": null,
      //       "vnote10": "pay",
      //       "createTime": 1556503708000,
      //       "updateTime": 1556503708000,
      //       "xyvote": "<p>1</p>",
      //       "isPay": null
      //     }, {
      //       "agreementId": 33,
      //       "activityId": 23,
      //       "typeId": 2,
      //       "corpId": "ding42d297c548f1006335c2f4657eb6378f",
      //       "title": "22",
      //       "isDel": 0,
      //       "vnote1": null,
      //       "vnote2": null,
      //       "vnote3": null,
      //       "vnote4": null,
      //       "vnote5": null,
      //       "vnote6": null,
      //       "vnote7": null,
      //       "vnote8": null,
      //       "vnote9": null,
      //       "vnote10": "pay",
      //       "createTime": 1559013967000,
      //       "updateTime": 1559013967000,
      //       "xyvote": "<p>22</p>",
      //       "isPay": null
      //     }],
      //     "body": [{
      //       "pricegroupbId": 51,
      //       "pricegroupId": 11,
      //       "ticketId": 20,
      //       "exclusive": null,
      //       "vnote": null,
      //       "updateTime": 1559110361000,
      //       "createTime": 1554980315000,
      //       "isDel": 0,
      //       "status": 0,
      //       "enote": "折扣标签",
      //       "snote": "价格标签",
      //       "inum": 97,
      //       "snum": 1,
      //       "eprice": 100,
      //       "sprice": 200.0,
      //       "ableNum": null,
      //       "frozenNum": 2,
      //       "tname": "aa",
      //       "detail": "[\"aa\",\"assa\",\"as\",\"asasazczcs\",\"啊啊啊啊啊啊啊啊\",\"啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊\",\"啊啊啊啊啊啊啊啊啊啊\",\"啊啊啊啊啊啊啊啊啊啊\"]",
      //       "address": "aaa",
      //       "stime": 1554566400000,
      //       "etime": 1555344000000,
      //       "tTypeDate": "06:04-06:10",
      //       "ticketUrl": "https://static.dingtalk.com/media/lALPDeC2t_ucqWvNAQ7NAQ4_270_270.png"
      //     }, {
      //       "pricegroupbId": 61,
      //       "pricegroupId": 11,
      //       "ticketId": 55,
      //       "exclusive": null,
      //       "vnote": null,
      //       "updateTime": 1559110361000,
      //       "createTime": 1556503750000,
      //       "isDel": 0,
      //       "status": 0,
      //       "enote": "折扣标签",
      //       "snote": "价格标签2",
      //       "inum": 98,
      //       "snum": 1,
      //       "eprice": 200,
      //       "sprice": 300.0,
      //       "ableNum": null,
      //       "frozenNum": 1,
      //       "tname": "票名",
      //       "detail": "[\"地址\",\"地址\"]",
      //       "address": "地址",
      //       "stime": 1555430400000,
      //       "etime": 1556121600000,
      //       "tTypeDate": "06:03-06:46",
      //       "ticketUrl": "https://static.dingtalk.com/media/lALPDgQ9qphGYxzNAoDNAoI_642_640.png"
      //     }, {
      //       "pricegroupbId": 61,
      //       "pricegroupId": 11,
      //       "ticketId": 56,
      //       "exclusive": null,
      //       "vnote": null,
      //       "updateTime": 1559110361000,
      //       "createTime": 1556503750000,
      //       "isDel": 0,
      //       "status": 0,
      //       "enote": "折扣标签2",
      //       "snote": "价格标签2",
      //       "inum": 98,
      //       "snum": 1,
      //       "eprice": 200,
      //       "sprice": 300.0,
      //       "ableNum": null,
      //       "frozenNum": 1,
      //       "tname": "票名",
      //       "detail": "[\"地址\",\"地址\"]",
      //       "address": "地址",
      //       "stime": 1555430400000,
      //       "etime": 1556121600000,
      //       "tTypeDate": "06:03-06:46",
      //       "ticketUrl": "https://static.dingtalk.com/media/lALPDgQ9qphGYxzNAoDNAoI_642_640.png"
      //     }, {
      //       "pricegroupbId": 61,
      //       "pricegroupId": 11,
      //       "ticketId": 57,
      //       "exclusive": null,
      //       "vnote": null,
      //       "updateTime": 1559110361000,
      //       "createTime": 1556503750000,
      //       "isDel": 0,
      //       "status": 0,
      //       "enote": "折扣标签2",
      //       "snote": "价格标签2",
      //       "inum": 98,
      //       "snum": 1,
      //       "eprice": 200,
      //       "sprice": 300.0,
      //       "ableNum": null,
      //       "frozenNum": 1,
      //       "tname": "票名",
      //       "detail": "[\"地址\",\"地址\"]",
      //       "address": "地址",
      //       "stime": 1555430400000,
      //       "etime": 1556121600000,
      //       "tTypeDate": "06:03-06:46",
      //       "ticketUrl": "https://static.dingtalk.com/media/lALPDgQ9qphGYxzNAoDNAoI_642_640.png"
      //     }]
      //   },
      //   "arguments": null
      // }


      // if (resData.success) {
      //   if (resData.result.length !== 0) {
      //     resolveData.success = true;
      //     let dataArrOld = [resData.result];
      //     let dataArr = [];
      //     for (let i = 0; i < dataArrOld.length; i++) {
      //       let obj = dataArrOld[i];
      //       let body = JSON.parse(JSON.stringify(obj.body));
      //       let arr = [];
      //       for (let j = 0; j < body.length; j++) {
      //         if (body[j].status === 0) {
      //           arr.push(body[j]);
      //         }
      //       }
      //       obj.body = arr;
      //       dataArr.push(obj);
      //     }
      //     let inumSum = 0; //库存总量
      //     let snumSum = 0; //销售总量
      //     for (let i = 0; i < dataArr.length; i++) {


      //       // 复制且存储防止其它页面刷新数据丢失
      //       $store.state.buyTicket.activityId = dataArr[i].head.projectId; //活动ID 
      //       sessionStorage.setItem('activityIdUcan', $store.state.buyTicket.activityId);

      //       // $store.state.buyTicket.ticketTypeId = dataArr[i].head.pricegroupId; //票类型id
      //       // sessionStorage.setItem('ticketTypeIdUcan', $store.state.buyTicket.ticketTypeId);

      //       // if (typeof dataArr[i].agereeId == 'string') {
      //       //   $store.state.buyTicket.agereeId = dataArr[i].agereeId.split(",")[0]; //《购票服务协议》
      //       // } else {
      //       //   $store.state.buyTicket.agereeId = JSON.parse(dataArr[i].agereeId)[0]; //《购票服务协议》
      //       // }
      //       // sessionStorage.setItem('agereeIdUcan', $store.state.buyTicket.agereeId);

      //       let agereeMents = [];
      //       for (let a = 0; a < dataArr[i].agereeMents.length; a++) {
      //         let obj = {
      //           title: dataArr[i].agereeMents[a].title,
      //           agreementId: dataArr[i].agereeMents[a].agreementId,
      //           typeId: dataArr[i].agereeMents[a].typeId
      //         }
      //         agereeMents.push(obj);
      //       }

      //       sessionStorage.setItem('agereeMents', JSON.stringify(agereeMents)); //防止其他页面刷新，所有只能存所有的数据


      //       $store.state.buyTicket.corpId = dataArr[i].corpId; //组织corpId,
      //       sessionStorage.setItem('corpIdUcan', $store.state.buyTicket.corpId);



      //       for (let j = 0; j < dataArr[i].body.length; j++) {

      //         // 写死默认多张票都对应一张表单
      //         if (!$store.state.buyTicket.ticketTypeId) {
      //           $store.state.buyTicket.ticketTypeId = dataArr[i].body[j].ticketId; //票id
      //           sessionStorage.setItem('ticketTypeIdUcan', $store.state.buyTicket.ticketTypeId);
      //         }


      //         // 如果没有原价，则赋值0
      //         if (!dataArr[i].body[j].sprice) {
      //           dataArr[i].body[j].sprice = 0;
      //         }
      //         // 处理没有且非0折扣价格的时候，折扣价就是原价
      //         if (dataArr[i].body[j].eprice === null || dataArr[i].body[j].eprice === '') {
      //           dataArr[i].body[j].eprice = dataArr[i].body[j].sprice;
      //         }
      //         if (Number(dataArr[i].body[j].inum) >= 0) {
      //           inumSum += Number(dataArr[i].body[j].inum) || 0;
      //         }
      //         snumSum += Number(dataArr[i].body[j].snum) || 0;
      //       }
      //     }
      //     // 判断是否已经已售罄
      //     if (inumSum <= 0) {
      //       $store.state.buyTicket.soldOut = true;
      //     } else {
      //       $store.state.buyTicket.soldOut = false;
      //     }
      //     // $store.state.buyTicket.soldOut = true; //为了测试
      //     resolveData.result = dataArr;
      //     // $store.state.buyTicket.ticketList = resolveData.result;




      //     // 特殊要求-特定票进行排序排序
      //     const ticketIdList = [192, 180, 193, 181, 186, 189, 194, 182, 187, 190, 195, 183, 188, 191, 196, 184, 185];
      //     $store.state.buyTicket.ticketList = [];
      //     for (let index = 0; index < resolveData.result.length; index++) {
      //       let obj = {
      //         ...resolveData.result[index]
      //       }
      //       obj.body = [];
      //       $store.state.buyTicket.ticketList.push(obj);
      //       $store.state.buyTicket.ticketList[index].body = [];
      //       let aginPostion = []; //需要进行排序的数组
      //       let otherArr = []; //不需要进行排序的数组
      //       for (let sonIndex = 0; sonIndex < resolveData.result[index].body.length; sonIndex++) {
      //         if (ticketIdList.indexOf(resolveData.result[index].body[sonIndex].ticketId) != -1) {
      //           resolveData.result[index].body[sonIndex].indexPostion = ticketIdList.indexOf(resolveData.result[index].body[sonIndex].ticketId);
      //           aginPostion.push(resolveData.result[index].body[sonIndex]);
      //         } else {
      //           otherArr.push(resolveData.result[index].body[sonIndex]);
      //         }
      //       }

      //       var compare = function (prop) {
      //         return function (obj1, obj2) {
      //           var val1 = obj1[prop];
      //           var val2 = obj2[prop];
      //           if (val1 < val2) {
      //             return -1;
      //           } else if (val1 > val2) {
      //             return 1;
      //           } else {
      //             return 0;
      //           }
      //         }
      //       }
      //       $store.state.buyTicket.ticketList[index].body = aginPostion.sort(compare("indexPostion")).concat(otherArr);
      //     }
      //     sessionStorage.setItem('ticketListUcan', JSON.stringify($store.state.buyTicket.ticketList));
      //   } else {
      //     resolveData.errorMsg = '未获取到相关数据';
      //   }
      // } else {
      //   resolveData.errorMsg = resData.errorMsg || '数据获取失败！';
      // }
      // resolve(resolveData);
      // return;
      let url = `ticketPurchaseMob/queryTicketInfoList?linkId=${linkId}&isPay=pay`
      axios.get(url).then(resData => {
        if (resData.success) {
          if (resData.result.length !== 0) {
            resolveData.success = true;
            let dataArrOld = [resData.result];
            let dataArr = [];
            for (let i = 0; i < dataArrOld.length; i++) {
              let obj = dataArrOld[i];
              let body = JSON.parse(JSON.stringify(obj.body));
              let arr = [];
              for (let j = 0; j < body.length; j++) {
                if (body[j].status === 0) {
                  arr.push(body[j]);
                }
              }
              obj.body = arr;
              dataArr.push(obj);
            }
            let inumSum = 0; //库存总量
            let snumSum = 0; //销售总量
            for (let i = 0; i < dataArr.length; i++) {


              // 复制且存储防止其它页面刷新数据丢失
              $store.state.buyTicket.activityId = dataArr[i].head.projectId; //活动ID 
              sessionStorage.setItem('activityIdUcan', $store.state.buyTicket.activityId);

              // $store.state.buyTicket.ticketTypeId = dataArr[i].head.pricegroupId; //票类型id
              // sessionStorage.setItem('ticketTypeIdUcan', $store.state.buyTicket.ticketTypeId);

              // if (typeof dataArr[i].agereeId == 'string') {
              //   $store.state.buyTicket.agereeId = dataArr[i].agereeId.split(",")[0]; //《购票服务协议》
              // } else {
              //   $store.state.buyTicket.agereeId = JSON.parse(dataArr[i].agereeId)[0]; //《购票服务协议》
              // }
              // sessionStorage.setItem('agereeIdUcan', $store.state.buyTicket.agereeId);

              let agereeMents = [];
              for (let a = 0; a < dataArr[i].agereeMents.length; a++) {
                let obj = {
                  title: dataArr[i].agereeMents[a].title,
                  agreementId: dataArr[i].agereeMents[a].agreementId,
                  typeId: dataArr[i].agereeMents[a].typeId
                }
                agereeMents.push(obj);
              }

              sessionStorage.setItem('agereeMents', JSON.stringify(agereeMents)); //防止其他页面刷新，所有只能存所有的数据


              $store.state.buyTicket.corpId = dataArr[i].corpId; //组织corpId,
              sessionStorage.setItem('corpIdUcan', $store.state.buyTicket.corpId);



              for (let j = 0; j < dataArr[i].body.length; j++) {

                // 写死默认多张票都对应一张表单
                if (!$store.state.buyTicket.ticketTypeId) {
                  $store.state.buyTicket.ticketTypeId = dataArr[i].body[j].ticketId; //票id
                  sessionStorage.setItem('ticketTypeIdUcan', $store.state.buyTicket.ticketTypeId);
                }


                // 如果没有原价，则赋值0
                if (!dataArr[i].body[j].sprice) {
                  dataArr[i].body[j].sprice = 0;
                }
                // 处理没有且非0折扣价格的时候，折扣价就是原价
                if (dataArr[i].body[j].eprice === null || dataArr[i].body[j].eprice === '') {
                  dataArr[i].body[j].eprice = dataArr[i].body[j].sprice;
                }
                if (Number(dataArr[i].body[j].inum) >= 0) {
                  inumSum += Number(dataArr[i].body[j].inum) || 0;
                }
                snumSum += Number(dataArr[i].body[j].snum) || 0;
              }
            }
            // 判断是否已经已售罄
            if (inumSum <= 0) {
              $store.state.buyTicket.soldOut = true;
            } else {
              $store.state.buyTicket.soldOut = false;
            }
            // $store.state.buyTicket.soldOut = true; //为了测试
            resolveData.result = dataArr;
            // $store.state.buyTicket.ticketList = resolveData.result;


            // 特殊要求-特定票进行排序排序
            // 门票排序：
            // 1. vip早鸟票 ID:192
            // 2. vip正价 ID:180
            // 3. 25日早鸟票 ID:193
            // 4. 25日正价 ID:181
            // 5. 25日浦发早鸟 ID:186
            // 6. 25日浦发正价 ID:189
            // 7. 26日早鸟票 ID:194
            // 8. 26日正价 ID:182
            // 9. 26日浦发早鸟   ID:187
            // 10.26日浦发正价   ID:190
            // 11. 27日早鸟票 ID:195
            // 12. 27日正价 ID:183
            // 13. 27日浦发早鸟 ID:188
            // 14. 27日浦发正价 ID:191
            // 15. 3日通早鸟 ID:196
            // 16. 3日通正价   ID:184
            // 17. 青年票 ID:185
            const ticketIdList = [192, 180, 193, 181, 186, 189, 194, 182, 187, 190, 195, 183, 188, 191, 196, 184, 185];
            $store.state.buyTicket.ticketList = [];
            for (let index = 0; index < resolveData.result.length; index++) {
              let obj = {
                ...resolveData.result[index]
              }
              obj.body = [];
              $store.state.buyTicket.ticketList.push(obj);
              $store.state.buyTicket.ticketList[index].body = [];
              let aginPostion = []; //需要进行排序的数组
              let otherArr = []; //不需要进行排序的数组
              for (let sonIndex = 0; sonIndex < resolveData.result[index].body.length; sonIndex++) {
                if (ticketIdList.indexOf(resolveData.result[index].body[sonIndex].ticketId) != -1) {
                  resolveData.result[index].body[sonIndex].indexPostion = ticketIdList.indexOf(resolveData.result[index].body[sonIndex].ticketId);
                  aginPostion.push(resolveData.result[index].body[sonIndex]);
                } else {
                  otherArr.push(resolveData.result[index].body[sonIndex]);
                }
              }

              var compare = function (prop) {
                return function (obj1, obj2) {
                  var val1 = obj1[prop];
                  var val2 = obj2[prop];
                  if (val1 < val2) {
                    return -1;
                  } else if (val1 > val2) {
                    return 1;
                  } else {
                    return 0;
                  }
                }
              }
              $store.state.buyTicket.ticketList[index].body = aginPostion.sort(compare("indexPostion")).concat(otherArr);
            }
            sessionStorage.setItem('ticketListUcan', JSON.stringify($store.state.buyTicket.ticketList));
          } else {
            resolveData.errorMsg = '未获取到相关数据';
          }
        } else {
          resolveData.errorMsg = resData.errorMsg || '数据获取失败！';
        }
        resolve(resolveData);
      }, err => {
        resolveData.errorMsg = '服务器出小差了！';
        resolve(resolveData);
      });
    });
  }
  // 得到协议 corpId, agereeId   //typeId 1 购票协议 2 露脸协议 3 登陆协议
  getAgreement(subData) {
    return new Promise((resolve) => {
      let resolveData = {
        success: false,
        errorMsg: '',
        result: []
      };
      axios.post('agreeMent/queryAgreeByStrIds', subData).then(
        resData => {
          if (resData.success) {
            resolveData.success = true;
            resolveData.result = resData.result;
          } else {
            resolveData.errorMsg = resData.errorMsg || '请重试';
          }
          resolve(resolveData);
        },
        err => {
          resolveData.errorMsg = '服务器出小差了！';
          resolve(resolveData);
        }
      );
    });
  };
  // 查看当前协议的详情
  lookAgreement(agreementId) {
    return new Promise((resolve) => {
      let resolveData = {
        success: false,
        errorMsg: '',
        result: []
      };
      let url = `signUp/queryAgreeUrl?corpId=${sessionStorage.getItem("corpIdUcan")}&agereeId=${agreementId}`
      axios.get(url).then(
        resData => {
          if (resData.success) {
            resolveData.success = true;
            resolveData.result = resData.result;
          } else {
            resolveData.errorMsg = resData.errorMsg || '请重试';
          }
          resolve(resolveData);
        },
        err => {
          resolveData.errorMsg = '服务器出小差了！';
          resolve(resolveData);
        }
      );
    });
  };
  //购票页用户预约开售提醒接口
  // countryCode:国家区号 mobile:手机号 activityId:项目主键
  reminderSale(countryCode, mobile, activityId) {
    return new Promise((resolve) => {
      let resolveData = {
        success: false,
        errorMsg: '',
        result: []
      };
      let subData = {
        countryCode: countryCode,
        mobile: mobile,
        projectId: activityId
      };
      axios.post('ticket/saleRemind/subscribe', subData).then(
        resData => {
          if (resData.success) {
            resolveData.success = true;
            resolveData.result = resData.result;
          } else {
            resolveData.errorMsg = resData.errorMsg || '请重试';
          }
          resolve(resolveData);
        },
        err => {
          resolveData.errorMsg = '服务器出小差了！';
          resolve(resolveData);
        }
      );
    });
  };
  // 得到《购票服务协议》链接接口
  getAgeree(corpId, agereeId) {
    return new Promise((resolve) => {
      let resolveData = {
        success: false,
        errorMsg: '',
        result: []
      };
      let subData = `?corpId=${corpId}&agereeId=${agereeId}`;
      axios.get('signUp/queryAgreeUrl' + subData).then(resData => {
        if (resData.success) {
          resolveData.success = true;
          resolveData.result = resData.result;
        } else {
          resolveData.errorMsg = resData.errorMsg || '数据获取失败！';
        }
        resolve(resolveData);
      }, err => {
        resolveData.errorMsg = '服务器出小差了！';
        resolve(resolveData);
      });
    });
  };
  // 同意并付款生成订单
  checkPay(carTicketList) {
    return new Promise((resolve) => {
      let resolveData = {
        success: false,
        errorMsg: '',
        result: []
      };

      // {
      //   "body_ticket": 
      //   [{
      //   "mny": "0.01",
      //   "num": "1",
      //   "price": "0.1",
      //   "pricegroupId": "66",
      //   "pricegroupbId": "274",
      //   "ticketId": "114",
      //   "tname": "票类型名称",
      //   "ticket_psn": [{"id": "4338"}]
      //   },
      //   {
      //   "mny": "0.01",
      //   "num": "1",
      //   "price": "0.01",
      //   "pricegroupId": "66",
      //   "pricegroupbId": "276",
      //   "ticketId": "116",
      //   "tname": "票类型名称",
      //   "ticket_psn": [{"id": "4340"}]
      //   }
      //   ]
      //   ,
      //   "head": {
      //   "createTime": "1557735934000", //不用改
      //   "isDel": "0",//不用改
      //   "pricegroupId": "66",//不用改
      //   "projectId": "27",//不用改
      //   "ptype": "0",//不用改
      //   "status": "0",//不用改
      // "updateTime": "1557829442000",//不用改
      // ==
      //   "orderType" : 1,ok //0是个人 1是团购
      //   "pname": "团购负责人姓名",ok// 如果是个人票则是原来值(价格组名称),如果是团购票则是团购负责人姓名；
      //   "cname": "公司名称",//ok
      //   "pricegroupName": "价格组名称",ok
      //   "total": 2,//ok
      //   "totalCurrentPrice": 0.02 ///ok
      //   }

      let subData = {};
      // head 部分
      if ($store.state.buyTicket.ticketList.length !== 0) {
        subData.head = JSON.parse(JSON.stringify($store.state.buyTicket.ticketList[0].head));
      } else {
        let ticketList = JSON.parse(sessionStorage.getItem('ticketListUcan')) || [];
        subData.head = JSON.parse(JSON.stringify(ticketList[0].head));
      }


      subData.head.pricegroupName = subData.head.pname; //价格组名称 注意要放在上面，因为下面是团购票将会改变subData.head.pname的值；
      subData.head.orderType = $store.state.buyTicket.orderType; //订单类型 0是个人 1是团购
      if (subData.head.orderType == 1) {
        // 如果是团购票，则pname价格组名称改成负责人姓名
        subData.head.pname = $store.state.buyTicket.cUserName;
        subData.head.cname = $store.state.buyTicket.cname; //团购票负责人公司名称
      }

      subData.head.total = $store.state.buyTicket.carTicketSumList.length; //总数量
      subData.head.totalCurrentPrice = Number($store.state.buyTicket.actualPrice).toFixed(2); //总价格 



      //  body_ticket 部分
      let body_ticket = [];
      for (let i = 0; i < carTicketList.length; i++) {
        let obj = {
          'mny': (Number(carTicketList[i].body_ticket[0].eprice) * carTicketList[i].body_ticket.length).toFixed(2),
          'num': carTicketList[i].body_ticket.length,
          'price': Number(carTicketList[i].body_ticket[0].eprice).toFixed(2),
          'pricegroupId': carTicketList[i].body_ticket[0].pricegroupId,
          'pricegroupbId': carTicketList[i].body_ticket[0].pricegroupbId,
          'ticketId': carTicketList[i].body_ticket[0].ticketId,
          "tname": carTicketList[i].body_ticket[0].tname,
          "ticket_psn": []
        }
        if (subData.head.orderType == 0) {
          //如果是个票则需要填写用票人信息
          for (let j = 0; j < carTicketList[i].useTicketPople.length; j++) {
            obj.ticket_psn.push({
              id: carTicketList[i].useTicketPople[j].userData.id
            });
          }
        }
        body_ticket.push(obj);
      }
      subData.body_ticket = body_ticket;
      console.log(subData);
      axios.post('ticketPurchaseMob/addTicketOrder', subData).then(
        resData => {
          if (resData.success) {
            resolveData.success = true;
            resolveData.result = resData.result;
          } else {
            resolveData.errorMsg = resData.errorMsg || '请重试';
          }
          resolve(resolveData);
        },
        err => {
          resolveData.errorMsg = '服务器出小差了！';
          resolve(resolveData);
        }
      );
    });
  };
  // 得到购物车中的票列表数据
  getCarTicketList() {
    return new Promise((resolve) => {
      let resolveData = {
        success: false,
        errorMsg: '',
        result: []
      };

      //没有购物车暂存的。默认查出空
      resolveData.success = true;
      resolveData.result = [];
      $store.state.buyTicket.carTicketList = resolveData.result;

      // 进行数据拆分，一共有多少张票
      $store.state.buyTicket.carTicketSumList = [];
      let carTicketList = $store.state.buyTicket.carTicketList;
      for (let i = 0; i < carTicketList.length; i++) {
        for (let j = 0; j < carTicketList[i].body_ticket.length; j++) {
          $store.state.buyTicket.carTicketSumList.push(
            carTicketList[i].body_ticket[j]
          );
        }
      }
      resolve(resolveData);
      // axios.get('ticketPurchaseMob/queryTicketOrderByPsnId').then(resData => {
      //   if (resData.success) {
      //     resolveData.success = true;
      //     resolveData.result = resData.result;
      //     $store.state.buyTicket.carTicketList = resolveData.result;

      //     // 进行数据拆分，一共有多少张票
      //     $store.state.buyTicket.carTicketSumList = [];
      //     let carTicketList = $store.state.buyTicket.carTicketList;
      //     for (let i = 0; i < carTicketList.length; i++) {
      //       for (let j = 0; j < carTicketList[i].body_ticket.length; j++) {
      //         $store.state.buyTicket.carTicketSumList.push(
      //           carTicketList[i].body_ticket[j]
      //         );
      //       }
      //     }
      //     // console.log($store.state.buyTicket.carTicketSumList);
      //   } else {
      //     resolveData.errorMsg = resData.errorMsg || '请重试';
      //   }
      //   resolve(resolveData);
      // }, err => {
      //   resolveData.errorMsg = '服务器出小差了！';
      //   resolve(resolveData);
      // });
    });
  }
  // 更新数据到购物车添加到服务器保存
  upDataCarTicketService(ticket) {
    return new Promise((resolve) => {
      let resolveData = {
        success: false,
        errorMsg: '',
        result: []
      };
      axios.post('ticketPurchaseMob/updateTicketOrder', ticket).then(resData => {
        if (resData.success) {
          resolveData.success = true;
          resolveData.result = resData.result;
        } else {
          resolveData.errorMsg = resData.errorMsg || '请重试';
        }
        resolve(resolveData);
      }, err => {
        resolveData.errorMsg = '服务器出小差了！';
        resolve(resolveData);
      });
    });
  }
}

export default new buyTicket();
